﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.Business.UsersAndSecurity;

namespace MyCommerce.Presentation.UsersAndSecurity.Presenters
{
    public class RolesAdministrationPresenter
    {
        private IRolesAdministrationView pViewToPresent;

        public RolesAdministrationPresenter(IRolesAdministrationView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCreate += new EventHandler(pViewToPresent_OnCreate);
            pViewToPresent.OnDelete += new GenericEventHandler<Role>(pViewToPresent_OnDelete);
            pViewToPresent.OnEdit += new GenericEventHandler<Role>(pViewToPresent_OnEdit);
            pViewToPresent.OnLoadView += new GenericEventHandler<long>(pViewToPresent_OnLoadView);
        }

        private void LoadUserList()
        {
            RoleManager employeeMgr = new RoleManager();
            pViewToPresent.ColItemsList = employeeMgr.GetRoleActives();
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<long> e)
        {
            LoadUserList();
        }

        private void pViewToPresent_OnEdit(object sender, GenericEventArgs<Role> e)
        {
            bool result = pViewToPresent.InvokeEditionView(e.EventData);
            if (result)
                LoadUserList();
        }

        private void pViewToPresent_OnDelete(object sender, GenericEventArgs<Role> e)
        {
            try
            {
                RoleManager userMgr = new RoleManager(true);
                userMgr.DeleteRole(e.EventData);
                LoadUserList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnCreate(object sender, EventArgs e)
        {
            bool result = pViewToPresent.InvokeEditionView(null);
            if (result)
                LoadUserList();
        }
    }
}
